package com.weiyuwqj.admin.controller.admin.account;

import com.weiyuwqj.admin.controller.base.BaseController;
import com.weiyuwqj.admin.controller.admin.log.annotation.LogAnnotation;
import com.weiyuwqj.api.service.BusAccountRechargeService;
import com.weiyuwqj.api.service.SysUserService;
import com.weiyuwqj.api.service.BasUserService;
import com.weiyuwqj.api.common.Paginator;
import com.weiyuwqj.api.constant.Message;
import com.weiyuwqj.api.exception.CrowdException;
import com.weiyuwqj.api.model.convert.*;
import com.weiyuwqj.api.model.vo.BusAccountRechargeVo;
import com.weiyuwqj.api.model.vo.SysUserVo;
import com.weiyuwqj.api.model.vo.UserVo;
import com.weiyuwqj.api.util.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


/**
 * The function of saving, modifying and deleting the site module
 * <p/>
 * Copyright 2015-2019 Yunfucloud technology co., Ltd.
 * Created by hanling on 15/10/24.
 */
@Controller
@RequestMapping("/admin/accountrecharge")
public class AccountRechargeController extends BaseController {

    @Autowired
    BusAccountRechargeService busAccountRechargeService;

    @Autowired
    BasUserService userService;

    @Autowired
    SysUserService sysUserService;

    /**
     * 根据订单号来修改订单的状态
     *
     * @param orderNo   订单号
     * @param oldStatus 旧status
     * @param newStatus 新status
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @LogAnnotation(module = "账户管理/充值管理", description = "根据订单号来修改订单的状态")
    public Object updateStatus(String orderNo, int oldStatus, int newStatus) {
        String addIp = IpUtils.get();
        busAccountRechargeService.updateBusAccountRechargeStatus(orderNo, oldStatus, newStatus, addIp);

        return Message.OK;
    }

    /**
     * 列出所有充值记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(
            @ModelAttribute Paginator p,
            @RequestParam(defaultValue = "") String userId,
            @RequestParam(defaultValue = "") String commonCond,
            @RequestParam(defaultValue = "0") int type,
            @RequestParam(defaultValue = "-1") int status, HttpServletRequest request) {
        String beginDateTime = request.getParameter("beginDateTime");
        String endDateTime = request.getParameter("endDateTime");
        String orderName = request.getParameter("orderName");
        String orderWay = request.getParameter("orderWay");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("userId", userId);
        map.put("commonCond", commonCond);
        map.put("type", type);
        map.put("status", status);
        if (StringUtils.isNotEmpty(beginDateTime)) {
            map.put("beginDateTime", beginDateTime);
        }
        if (StringUtils.isNotEmpty(endDateTime)) {
            map.put("endDateTime", endDateTime);
        }
        if (StringUtils.isNotEmpty(orderName)) {
            map.put("orderName", orderName);
        }
        if (StringUtils.isNotEmpty(orderWay)) {
            map.put("orderWay", orderWay);
        }
        return busAccountRechargeService.getList(p, map);
    }

    /**
     * 用户充值导出
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/excel/accountRecharge")
    public Object accountRechargeExcel(HttpServletRequest request,
                                       String userId,
                                       String type,
                                       String status,
                                       String commonCond,
                                       String beginDateTime,
                                       String endDateTime) {

        String[] cArray = {"ID", "用户名", "真实姓名", "订单号", "充值金额", "到账金额", "手续费", "充值类型", "充值来源", "充值状态", "添加时间"};
        String[] eArray = {"Id", "Username", "Realname", "OrderNo", "RechargeAmount", "ActualAmount", "FeeAmount", "RechargeType", "RechargeResource", "RechargeStatus", "Addtime"};
        ModelConvert[] converts = {null, null, null, null, null, null, null, new AccountRechargeTypeConvert(), null,
                new AccountRechargeStatusConvert(), new AccountRechargeAddtimeConvert()};

        List<String> cList = Arrays.asList(cArray);
        List<String> eList = Arrays.asList(eArray);
        List<ModelConvert> convertList = Arrays.asList(converts);
        Map map = new HashMap();
        map.put("userId", userId);
        map.put("type", type);
        map.put("commonCond", commonCond);
        map.put("status", status);
        map.put("beginDateTime", beginDateTime);
        map.put("endDateTime", endDateTime);

        List<BusAccountRechargeVo> contentList = busAccountRechargeService.getList(map);

        String serverDir = request.getServletContext().getRealPath("/") + "admin/export/";
        String excelFilePath = ExcelUtils.export("充值管理", cList, eList, convertList, contentList, serverDir);
        HashMap ret = Message.OK;
        ret.put("file", "export/" + excelFilePath);
        return ret;
    }

    /*
     * 列出订单号记录记录
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getSingle", method = RequestMethod.POST)
    public Object getSingle(
            @RequestParam(defaultValue = "") String orderNo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("orderNo", orderNo);
        return busAccountRechargeService.getSingle(map);
    }



    /**
     * 根据查询条件统计充值
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/statRecharge", method = RequestMethod.POST)
    public Object statRecharge(@RequestParam(defaultValue = "") String userId,
                               @RequestParam(defaultValue = "") String commonCond,
                               @RequestParam(defaultValue = "0") int type,
                               @RequestParam(defaultValue = "-1") int status, HttpServletRequest request) {
        String beginDateTime = request.getParameter("beginDateTime");
        String endDateTime = request.getParameter("endDateTime");
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("userId", userId);
        queryMap.put("commonCond", commonCond);
        queryMap.put("type", type);
        queryMap.put("status", status);
        if (StringUtils.isNotEmpty(beginDateTime)) {
            queryMap.put("beginDateTime", beginDateTime);
        }
        if (StringUtils.isNotEmpty(endDateTime)) {
            queryMap.put("endDateTime", endDateTime);
        }
        Map<String, Object> map = busAccountRechargeService.sumRechargeAmount(queryMap);
        return map;
    }

    //判断手机号码是否存在
    @ResponseBody
    @RequestMapping(value = "/mobileIsExist", method = RequestMethod.POST)
    public Object mobileIsExist(String username) {
        SysUserVo sysUser;
        UserVo user;
        sysUser = sysUserService.getOneByUsername(username);
        user = userService.getUserByMobile(username);
        if (sysUser == null && user == null) {
            throw new CrowdException("user.forget.mobile_not_exist");
        }
        Map map = new HashMap();
        map.put("result", "success");
        if (user != null) {
            map.put("realname", user.getRealname());
        }
        if (sysUser != null) {
            map.put("realname", sysUser.getRealname());
        }

        return map;
    }

//    /**
//     * 平台充值
//     *
//     * @param amount
//     * @return
//     */
//    @ResponseBody
//    @RequestMapping(value = "/platformRecharge", method = RequestMethod.POST)
//    public Object platformRecharge(String amount, String remark) {
//        return busAccountRechargeService.platformRecharge(amount, "后台平台充值", IpUtils.get(), remark, 2);
//    }


}